import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] arr = new int[]{22,11,55,44,88,44,77,33};
        insertSort(arr);
        System.out.println(Arrays.toString(arr));

        int[] arr2 = new int[]{22,11,55,44,88,44,77,33};

        shellSort(arr2);
        System.out.println(Arrays.toString(arr2));

    }


    // 插入排序
    public static void insertSort(int[] array){
        for (int i = 1; i < array.length; i++) {
            int temp = array[i];
            int j = i - 1;
            for ( ; j >= 0; j--) {
                if(array[j] > temp){
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = temp;
        }
    }


        // 希尔排序
    public static void shellSort(int[] array){
         int gap = array.length;
         while(gap > 1){
             gap /= 2;
             shell(array,gap);
         }
    }

    private static void shell(int[] array,int gap){
        for (int i = gap; i < array.length; i++) {
            int temp = array[i];
            int j = i - gap;
            for ( ; j >= 0; j-=gap) {
                if(array[j] > temp){
                    array[j+gap] = array[j];
                }else {
                    break;
                }
            }
            array[j+gap] = temp;
        }
    }
}